package api

import (
	"gitee.com/love_bass/go-area"
	"gitee.com/love_bass/go-utils/convert"
	"github.com/gin-gonic/gin"
	"go-base/internal/global"
)

// QueryProvincesHandle
// @Summary 查询省列表
// @Description 返回全国所有省份
// @Tags 区域
// @Accept  json
// @Produce  json
// @Success 200
// @Router /api/areas/provinces [get]
// @security ApiKeyAuth
func QueryProvincesHandle(c *gin.Context) {
	// 获取客户端请求的HTTP版本
	httpVersion := c.Request.Proto
	region, _ := area.GetIpRegion(c.ClientIP())
	defer global.Console.Debug("客户端版本：%s；来源地址：%s；归属地：%+v", httpVersion, c.ClientIP(), region)

	results := area.GetProvinces()
	c.JSON(200, HttpResponse{
		Code:    200,
		Message: "查询成功",
		Data:    results,
	})
}

// QueryCitiesByProvinceIdHandle
// @Summary 查询市列表
// @Description 返回指定省份ID的所有市
// @Tags 区域
// @Param ProvinceID path string true "省ID"
// @Accept  json
// @Produce  json
// @Success 200
// @Router /api/areas/cities/{ProvinceID} [get]
// @security ApiKeyAuth
func QueryCitiesByProvinceIdHandle(c *gin.Context) {
	// 获取客户端请求的HTTP版本
	httpVersion := c.Request.Proto
	region, _ := area.GetIpRegion(c.ClientIP())
	defer global.Console.Debug("客户端版本：%s；来源地址：%s；归属地：%+v", httpVersion, c.ClientIP(), region)

	params := Params{Ctx: c}
	idStr := params.Param("ProvinceID")
	provinceId, err := convert.StringToUint64(idStr)
	if err != nil {
		c.JSON(400, HttpResponse{
			Code:    400,
			Message: "参数错误",
			Data:    "省ID必须是数字",
		})
		return

	}
	results := area.GetCitiesByProvinceID(provinceId)
	if len(results) == 0 {
		c.JSON(404, HttpResponse{
			Code:    404,
			Message: "无记录",
			Data:    nil,
		})
		return
	}
	c.JSON(200, HttpResponse{
		Code:    200,
		Message: "查询成功",
		Data:    results,
	})
}

// QueryCountiesByCiteIdHandle
// @Summary 查询县列表
// @Description 返回指定城市ID的所有县
// @Tags 区域
// @Param CityID path string true "市ID"
// @Accept  json
// @Produce  json
// @Success 200
// @Router /api/areas/counties/{CityID} [get]
// @security ApiKeyAuth
func QueryCountiesByCiteIdHandle(c *gin.Context) {
	// 获取客户端请求的HTTP版本
	httpVersion := c.Request.Proto
	region, _ := area.GetIpRegion(c.ClientIP())
	defer global.Console.Debug("客户端版本：%s；来源地址：%s；归属地：%+v", httpVersion, c.ClientIP(), region)

	params := Params{Ctx: c}
	idStr := params.Param("CityID")
	cityId, err := convert.StringToUint64(idStr)
	if err != nil {
		c.JSON(400, HttpResponse{
			Code:    400,
			Message: "参数错误",
			Data:    "市ID必须是数字",
		})
		return

	}
	results := area.GetCountiesByCityID(cityId)
	if len(results) == 0 {
		c.JSON(404, HttpResponse{
			Code:    404,
			Message: "无记录",
			Data:    nil,
		})
		return
	}
	c.JSON(200, HttpResponse{
		Code:    200,
		Message: "查询成功",
		Data:    results,
	})
}

// QueryUpperIdByAreaIdHandle
// @Summary 查询上级区域
// @Description 返回指定ID的上级区域，如果是省级区域，则返回自身
// @Tags 区域
// @Param AreaID path string true "区域ID"
// @Accept  json
// @Produce  json
// @Success 200
// @Router /api/areas/upper/{AreaID} [get]
// @security ApiKeyAuth
func QueryUpperIdByAreaIdHandle(c *gin.Context) {
	// 获取客户端请求的HTTP版本
	httpVersion := c.Request.Proto
	region, _ := area.GetIpRegion(c.ClientIP())
	defer global.Console.Debug("客户端版本：%s；来源地址：%s；归属地：%+v", httpVersion, c.ClientIP(), region)

	params := Params{Ctx: c}
	idStr := params.Param("AreaID")
	areaId, err := convert.StringToUint64(idStr)
	if err != nil {
		c.JSON(400, HttpResponse{
			Code:    400,
			Message: "参数错误",
			Data:    "区域ID必须是数字",
		})
		return
	}
	results, found := area.GetUpperArea(areaId)
	if !found {
		c.JSON(404, HttpResponse{
			Code:    404,
			Message: "无记录",
			Data:    nil,
		})
		return
	}
	c.JSON(200, HttpResponse{
		Code:    200,
		Message: "查询成功",
		Data:    results,
	})
}

// SearchProvinceByKewordHandle
// @Summary 搜索省（模糊查询）只返回第一条
// @Description 返回名称包含关键字的省（单条）
// @Tags 区域
// @Param Keyword path string true "省关键字"
// @Accept  json
// @Produce  json
// @Success 200
// @Router /api/areas/province/{Keyword} [get]
// @security ApiKeyAuth
func SearchProvinceByKewordHandle(c *gin.Context) {
	// 获取客户端请求的HTTP版本
	httpVersion := c.Request.Proto
	region, _ := area.GetIpRegion(c.ClientIP())
	defer global.Console.Debug("客户端版本：%s；来源地址：%s；归属地：%+v", httpVersion, c.ClientIP(), region)

	params := Params{Ctx: c}
	keyword := params.Param("Keyword")
	if keyword == "" {
		c.JSON(400, HttpResponse{
			Code:    400,
			Message: "参数错误",
			Data:    "关键字不能为空",
		})
		return

	}
	result, found := area.SearchProvinceByTitle(keyword)
	if !found {
		c.JSON(404, HttpResponse{
			Code:    400,
			Message: "无记录",
			Data:    nil,
		})
		return

	}
	c.JSON(200, HttpResponse{
		Code:    200,
		Message: "查询成功",
		Data:    result,
	})
}

// SearchProvincesByKewordHandle
// @Summary 搜索省（模糊查询）返回多条
// @Description 返回名称包含关键字的省（多条）
// @Tags 区域
// @Param Keyword path string true "省关键字"
// @Accept  json
// @Produce  json
// @Success 200
// @Router /api/areas/provinces/{Keyword} [get]
// @security ApiKeyAuth
func SearchProvincesByKewordHandle(c *gin.Context) {
	// 获取客户端请求的HTTP版本
	httpVersion := c.Request.Proto
	region, _ := area.GetIpRegion(c.ClientIP())
	defer global.Console.Debug("客户端版本：%s；来源地址：%s；归属地：%+v", httpVersion, c.ClientIP(), region)

	params := Params{Ctx: c}
	keyword := params.Param("Keyword")
	if keyword == "" {
		c.JSON(400, HttpResponse{
			Code:    400,
			Message: "参数错误",
			Data:    "关键字不能为空",
		})
		return

	}
	results := area.SearchProvincesByTitle(keyword)
	if len(results) == 0 {
		c.JSON(404, HttpResponse{
			Code:    404,
			Message: "无记录",
			Data:    nil,
		})
		return
	}
	c.JSON(200, HttpResponse{
		Code:    200,
		Message: "查询成功",
		Data:    results,
	})
}

// SearchCityByProvinceIdAndKeywordHandle
// @Summary 搜索市（模糊查询）只返回第一条
// @Description 返回指定ID的省下的名称包含关键字的市（单条）
// @Tags 区域
// @Param ProvinceID path uint64 true "省ID"
// @Param Keyword path string true "市关键字"
// @Accept  json
// @Produce  json
// @Success 200
// @Router /api/areas/city/{ProvinceID}/{Keyword} [get]
// @security ApiKeyAuth
func SearchCityByProvinceIdAndKeywordHandle(c *gin.Context) {
	// 获取客户端请求的HTTP版本
	httpVersion := c.Request.Proto
	region, _ := area.GetIpRegion(c.ClientIP())
	defer global.Console.Debug("客户端版本：%s；来源地址：%s；归属地：%+v", httpVersion, c.ClientIP(), region)

	params := Params{Ctx: c}
	keyword := params.Param("Keyword")
	if keyword == "" {
		c.JSON(400, HttpResponse{
			Code:    400,
			Message: "参数错误",
			Data:    "关键字不能为空",
		})
		return

	}
	idStr := params.Param("ProvinceID")
	provinceId, err := convert.StringToUint64(idStr)
	if err != nil {
		c.JSON(400, HttpResponse{
			Code:    400,
			Message: "参数错误",
			Data:    "省份ID必须是数字",
		})
		return

	}
	result, found := area.SearchCityByTitle(provinceId, keyword)
	if !found {
		c.JSON(404, HttpResponse{
			Code:    404,
			Message: "无记录",
			Data:    nil,
		})
		return
	}
	c.JSON(200, HttpResponse{
		Code:    200,
		Message: "查询成功",
		Data:    result,
	})
}

// SearchCitiesByProvinceIdAndKeywordHandle
// @Summary 搜索市（模糊查询）返回多条
// @Description 返回指定ID的省下的名称包含关键字的市（多条）
// @Tags 区域
// @Param ProvinceID path uint64 true "省ID"
// @Param Keyword path string true "市关键字"
// @Accept  json
// @Produce  json
// @Success 200
// @Router /api/areas/cities/{ProvinceID}/{Keyword} [get]
// @security ApiKeyAuth
func SearchCitiesByProvinceIdAndKeywordHandle(c *gin.Context) {
	// 获取客户端请求的HTTP版本
	httpVersion := c.Request.Proto
	region, _ := area.GetIpRegion(c.ClientIP())
	defer global.Console.Debug("客户端版本：%s；来源地址：%s；归属地：%+v", httpVersion, c.ClientIP(), region)

	params := Params{Ctx: c}
	keyword := params.Param("Keyword")
	if keyword == "" {
		c.JSON(400, HttpResponse{
			Code:    400,
			Message: "参数错误",
			Data:    "关键字不能为空",
		})
		return

	}
	idStr := params.Param("ProvinceID")
	provinceId, err := convert.StringToUint64(idStr)
	if err != nil {
		c.JSON(400, HttpResponse{
			Code:    400,
			Message: "参数错误",
			Data:    "省份ID必须是数字",
		})
		return

	}
	results := area.SearchCitiesByTitle(provinceId, keyword)
	if len(results) == 0 {
		c.JSON(404, HttpResponse{
			Code:    404,
			Message: "无记录",
			Data:    nil,
		})
		return
	}
	c.JSON(200, HttpResponse{
		Code:    200,
		Message: "查询成功",
		Data:    results,
	})
}
